<?php

class ModelBuilder
{

    static function buildRootModel($schema, $c)
    {
        $deep = [];
        foreach ($schema as $key => $value) {
            if (is_array($value)) {
                $deep[$key] = self::buildObjectModel($value, [$key], $c);
            } else if ($value == 'array') {
                $deep[$key] = self::buildArrayModel([$key], $c);
            } else {
                $deep[$key] = self::buildScalarModel($value, [$key], $c);
            }
        }
        return new RootModel($deep, $c);
    }

    static function buildObjectModel($schema, $path, $c)
    {
        $deep = [];
        foreach ($schema as $key => $value) {
            $schema1 = $value;
            $path1 = $path;
            $path1[] = $key;
            if (is_array($value)) {
                $deep[$key] = self::buildObjectModel($schema1, $path1, $c);
            } else if ($value == 'array') {
                $deep[$key] = self::buildArrayModel($path1, $c);
            } else {
                $deep[$key] = self::buildScalarModel($schema1, $path1, $c);
            }
        }

        return new ObjectModel($deep, $path, $c);
    }

    static function buildArrayModel($path, $c)
    {
        return new ArrayModel($path, $c);
    }

    static function buildScalarModel($type, $path, $c)
    {
        return new ScalarModel($type, $path, $c);
    }

}